package cyou.breathe.blog.knowledgebase.application;

import cn.hutool.core.lang.Assert;
import cyou.breathe.blog.api.article.model.ArticleVO;
import cyou.breathe.blog.api.article.request.ArticleCreateRequest;
import cyou.breathe.blog.api.article.request.ArticleDeleteRequest;
import cyou.breathe.blog.api.article.request.ArticleQueryRequest;
import cyou.breathe.blog.api.article.request.ArticleUpdateRequest;
import cyou.breathe.blog.api.article.response.ArticleCreateResponse;
import cyou.breathe.blog.api.article.response.ArticleDeleteResponse;
import cyou.breathe.blog.api.article.response.ArticleQueryResponse;
import cyou.breathe.blog.api.article.response.ArticleUpdateResponse;
import cyou.breathe.blog.api.article.service.ArticleFacadeService;
import cyou.breathe.blog.api.folder.model.FolderVO;
import cyou.breathe.blog.api.folder.request.*;
import cyou.breathe.blog.api.folder.response.*;
import cyou.breathe.blog.api.folder.service.FolderFacadeService;
import cyou.breathe.blog.api.knowledgebase.request.KnowledgeBaseContentRequest;
import cyou.breathe.blog.api.knowledgebase.response.KnowledgeBaseContentResponse;
import cyou.breathe.blog.article.infrastructure.exception.ArticleErrorCode;
import cyou.breathe.blog.article.infrastructure.exception.ArticleException;
import cyou.breathe.blog.folder.infrastructure.exception.FolderErrorCode;
import cyou.breathe.blog.folder.infrastructure.exception.FolderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.logging.Logger;

/**
 * @author: breathe
 * @createTime: 2025-09-05
 */
@Service
public class KnowledgeBaseApplicationService {

    private static final Logger LOG = Logger.getLogger(KnowledgeBaseApplicationService.class.getName());

    @Autowired
    private FolderFacadeService folderFacadeService;

    @Autowired
    private ArticleFacadeService articleFacadeService;

    //通过考虑, 我认为这几个功能应该写在knowledge-base中

    /**
     * 创建根目录
     * @param request
     * @return
     */
    public FolderCreateRootResponse createRootFolder(FolderCreateRootRequest request) {
        return folderFacadeService.createRootFolder(request);
    }

    /**
     * 查询目录中所有目录和文章(不递归查询)
     * @param request
     * @return
     */
    public KnowledgeBaseContentResponse<FolderVO> queryFolder(KnowledgeBaseContentRequest request) {
        Long folderId = request.getFolderId();
        Long userId = request.getUserId();

        ArticleQueryRequest articleQueryRequest = new ArticleQueryRequest(folderId, userId);
        ArticleQueryResponse articleQueryResponse = articleFacadeService.queryArticles(articleQueryRequest);

        Assert.isTrue(articleQueryResponse.getSuccess(), () -> new ArticleException(ArticleErrorCode.ARTICLE_GET_FAILED));

        List<ArticleVO> articles = articleQueryResponse.getData();

        FolderQueryResponse<FolderVO> queryFolderVOResponse = folderFacadeService.getFolderById(new FolderQueryRequest(folderId, userId));

        FolderVO parent = queryFolderVOResponse.getData();
        parent.setArticles(articles);

        return new KnowledgeBaseContentResponse<>(parent);
    }

    /**
     * 查询目录中所有目录和文章(递归查询)
     * @param request
     * @return
     */
    public KnowledgeBaseContentResponse<FolderVO> queryAllFolder(KnowledgeBaseContentRequest request) {
        KnowledgeBaseContentResponse<FolderVO> response = new KnowledgeBaseContentResponse<>();

        Long folderId = request.getFolderId();
        Long userId = request.getUserId();

        FolderQueryResponse<FolderVO> queryFolderVOResponse = folderFacadeService.getFolderById(new FolderQueryRequest(folderId, userId));
        FolderVO root = queryFolderVOResponse.getData();

        fillFolderRecursively(root, userId);

        response.setData(root);
        response.setSuccess(true);

        return response;
    }

    private void fillFolderRecursively(FolderVO folder, Long userId) {
        // 查询当前目录下的子文件夹
        FolderQueryResponse<List<FolderVO>> sonResponse = folderFacadeService.getFolderByParentId(folder.getId(), userId);
        List<FolderVO> son = sonResponse.getData();
        folder.setChildren(son);

        // 查询当前目录下的文章
        ArticleQueryRequest articleQueryRequest = new ArticleQueryRequest(folder.getId(), userId);
        ArticleQueryResponse articleQueryResponse = articleFacadeService.queryArticles(articleQueryRequest);
        Assert.isTrue(articleQueryResponse.getSuccess(), () -> new ArticleException(ArticleErrorCode.ARTICLE_GET_FAILED));

        List<ArticleVO> articles = articleQueryResponse.getData();
        folder.setArticles(articles);
        // 递归对子文件夹做同样处理
        for (FolderVO child : son) {
            fillFolderRecursively(child, userId);
        }
    }
    //删除目录(这个得递归删除所有子目录和文章)
    @Transactional(rollbackFor = Exception.class)
    public KnowledgeBaseDeleteFolderResponse deleteAllFolder(Long folderId, Long userId) {
        KnowledgeBaseDeleteFolderResponse response = new KnowledgeBaseDeleteFolderResponse();
        ArticleDeleteByFolderIdRequest articleDeleteByFolderIdRequest = new ArticleDeleteByFolderIdRequest(folderId, userId);
        ArticleDeleteByFolderIdResponse articleDeleteByFolderIdResponse = articleFacadeService.deleteArticleByFolderId(articleDeleteByFolderIdRequest);

        Assert.isTrue(articleDeleteByFolderIdResponse.getSuccess(), () -> new ArticleException(ArticleErrorCode.ARTICLE_DELETE_FAILED));

        FolderQueryResponse<List<FolderVO>> folderByParentIdResponse = folderFacadeService.getFolderByParentId(folderId, userId);

        List<FolderVO> data = folderByParentIdResponse.getData();
        for (FolderVO folderVO : data) {
            KnowledgeBaseDeleteFolderResponse knowledgeBaseDeleteFolderResponse = deleteAllFolder(folderVO.getId(), userId);
        }

        FolderDeleteRequest folderDeleteRequest = new FolderDeleteRequest(folderId, userId);
        FolderDeleteResponse folderDeleteResponse = folderFacadeService.deleteFolder(folderDeleteRequest);

        Assert.isTrue(folderDeleteResponse.getSuccess(), () -> new FolderException(FolderErrorCode.FOLDER_DELETE_FAILED));

        response.setSuccess(true);
        response.setFolderId(folderId);

        return response;
    }

    /**
     * 创建文章
     * @param request
     * @return
     */
    public ArticleCreateResponse createArticle(ArticleCreateRequest request) {
        return articleFacadeService.createArticle(request);
    }

    /**
     * 删除文章
     * @param request
     * @return
     */
    public ArticleDeleteResponse deleteArticle(ArticleDeleteRequest request) {
        return articleFacadeService.deleteArticle(request);
    }

    /**
     * 修改文章
     * @param request
     * @return
     */
    public ArticleUpdateResponse updateArticle(ArticleUpdateRequest request) {
        return articleFacadeService.updateArticle(request);
    }

    /**
     * 修改目录
     * @param request
     * @return
     */
    public FolderUpdateResponse updateFolder(FolderUpdateRequest request) {
        return folderFacadeService.updateFolder(request);
    }

}
